package com.qdd.freight.service.impl;

import com.qdd.freight.config.HandlerContext;
import com.qdd.freight.entity.Datas;
import com.qdd.freight.handler.AbstractHandler;
import com.qdd.freight.service.FreightService;
import jdk.nashorn.internal.runtime.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * service实现类，根据不同的mode值最终获取对应的处理器实例，并调用自己实现的处理器方法进行逻辑计算
 *
 * @ClassName IOrderServiceImpl
 * @Description TODO
 * @Author yanpeihao
 * @Date 2019/12/6 21:21
 * @Version 1.0
 **/

@Service
@Logger
public class FreightServiceImpl implements FreightService {
    //注入处理器容器
    @Autowired
    private HandlerContext handlerContext;

    /**
     * @param modeId    快运类型
     * @param goodsId   商品id
     * @param entId     仓库id
     * @param quantity  采购数量
     * @param company   企业编号
     * @param consignee 省市区编号
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Datas handle(Integer modeId, Integer goodsId, Integer entId, Integer quantity,
                        Integer company, String consignee, Double boxVolume, Double singleVolume,
                        Double boxWeight, Double singleWeight, Integer QTYRate) throws Exception {
        //从spring容器中获取处理器实例
        AbstractHandler handlerInstance = handlerContext.getHandlerInstance(String.valueOf(modeId));
        //实例对象调用公共的计算逻辑方法进行运价计算
        Datas datas = handlerInstance.handelFreight(modeId, goodsId, entId, quantity, company, consignee,
                boxVolume, singleVolume, boxWeight, singleWeight, QTYRate);
        return datas;
    }
}
